package com.newbie.cellsms.base;

import java.io.UnsupportedEncodingException;

/**
 * -用于对消息的封包操作 +将utf8字符转成ucs2并最终输出16进制可打印字符
 * 
 * @author Jacarri, +Christ
 * 
 */
public class PackageUtil {
	private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
			'e', 'f' };

	private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
			'E', 'F' };

	public static String assemblePackage(String src) throws UnsupportedEncodingException {
		byte[] srcByte = src.getBytes("ISO-10646-UCS-2");
		String encodeStr = encodeHexStr(srcByte);
		return encodeStr;
	}

	public static String encodeHexStr(byte[] data) {
		return encodeHexStr(data, false);
	}

	public static String encodeHexStr(byte[] data, boolean toLowerCase) {
		return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
	}

	protected static String encodeHexStr(byte[] data, char[] toDigits) {
		return new String(encodeHex(data, toDigits));
	}

	protected static char[] encodeHex(byte[] data, char[] toDigits) {
		int l = data.length;
		char[] out = new char[l << 1];
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
			out[j++] = toDigits[0x0F & data[i]];
		}
		return out;
	}
}
